/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.colegiatura.beans;


import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.cip.esymons.colegiatura.bc.CargoDelegate;
import org.cip.esymons.general.be.Area;
import org.cip.esymons.general.be.Cargo;

/**
 *
 * @author ADMINISTRADOR
 */
public class cargoController {

    private CargoDelegate delegado;
    private Cargo cargo;
    private Area area;
    private Integer codarea;

    public cargoController() {

        delegado = new CargoDelegate();
        cargo = new Cargo();
        area = new Area();
       
    }

    public Integer getCodarea() {
        return codarea;
    }

    public void setCodarea(Integer codarea) {
        this.codarea = codarea;
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }
    
    public Cargo getCargo() {
        return cargo;
    }

    public void setCargo(Cargo cargo) {
        this.cargo = cargo;
    }

    public CargoDelegate getDelegado() {
        return delegado;
    }

    public void setDelegado(CargoDelegate delegado) {
        this.delegado = delegado;
    }

    public Cargo buscaPorCodigo(Integer codigo){

        return delegado.find(codigo);
    }

    public List<Cargo> getListaCargo(){

        return delegado.findAll();
    }

    public String nuevo(){
        cargo = new Cargo();
        return "ir_a_nuevo";
    }

    public String editar(){
        delegado.edit(cargo);
        cargo = new Cargo();
        mostrarMensaje("INFO: ", "Cargo actualizado satisfactoriamente...", FacesMessage.SEVERITY_INFO);
        return "ir_a_listar";
    }

    public String grabar(){
        delegado.create(cargo);
        cargo = new Cargo();
        mostrarMensaje("INFO: ", "Cargo registrado satisfactoriamente...", FacesMessage.SEVERITY_INFO);
        return null;
    }

    public String remover(){

        delegado.remove(cargo);
        cargo = new Cargo();
        return null;
    }

    //LISTADO DE CARGOS
   public SelectItem[] getSelectListaItem(){

      try{
//        System.out.println("Ingreso 1");
        SelectItem[] selects = null;
        List<Cargo> listaCargo = delegado.findAll();
        selects = new SelectItem[listaCargo.size()];
        int cont = 0;
        for(Cargo car : listaCargo){

            SelectItem item = new SelectItem(car, car.getCarDescripcion());
            selects[cont] = item;
            cont++;
        }
        return selects;
      }catch(Exception e){
            System.out.println("Ingreso Error");
            SelectItem[] selects = new SelectItem[1];
            SelectItem item = new SelectItem(null, "No existen cargos");
            selects[0] = item;
            return selects;
      }

    }

      public SelectItem[] getSelectListaCargosArea(){

      try{
//        System.out.println("Ingreso 1");
        SelectItem[] selects = null;
          System.out.println("area " + codarea);
        List<Cargo> listaCargo = delegado.buscaCargoPorArea(codarea);
        selects = new SelectItem[listaCargo.size()];
        int cont = 0;
        for(Cargo car : listaCargo){

            SelectItem item = new SelectItem(car, car.getCarDescripcion());
            selects[cont] = item;
            cont++;
        }
        return selects;
      }catch(Exception e){
            System.out.println("Ingreso Error");
            SelectItem[] selects = new SelectItem[1];
            SelectItem item = new SelectItem(null, "No existen cargos");
            selects[0] = item;
            return selects;
      }

    }


      public SelectItem[] getSelectListaCargoPorAreas(){

      try{
//        System.out.println("Ingreso 1");
        SelectItem[] selects = null;
          System.out.println("area " + area);
        List<Cargo> listaCargo = delegado.buscaCargosPorAreas(area);
        selects = new SelectItem[listaCargo.size()];
        int cont = 0;
        for(Cargo car : listaCargo){

            SelectItem item = new SelectItem(car, car.getCarDescripcion());
            selects[cont] = item;
            cont++;
        }
        return selects;
      }catch(Exception e){
            System.out.println("Ingreso Error");
            SelectItem[] selects = new SelectItem[1];
            SelectItem item = new SelectItem(null, "No existen cargos");
            selects[0] = item;
            return selects;
      }

    }

    public void mostrarMensaje(String resumen, String detalle, FacesMessage.Severity severidad) {
        try {
            FacesMessage fm = new FacesMessage(severidad, resumen, detalle);
            FacesContext.getCurrentInstance().addMessage(fm.toString(), fm);
        } catch (Exception e) {
            FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage());
            FacesContext.getCurrentInstance().addMessage(fm.toString(), fm);
        }
    }

}
